package com.kelvin.paopao.module.market.wallet.wallet;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kelvin.paopao.R;
import com.kelvin.paopao.injector.components.DaggerWalletComponent;
import com.kelvin.paopao.injector.modules.WalletModule;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.base.IBasePresenter;
import com.kelvin.paopao.module.market.wallet.charge.ChargeActivity;
import com.kelvin.paopao.module.market.wallet.detail.WalletDetailActivity;
import com.kelvin.paopao.module.market.wallet.recharge.RechargeActivity;
import com.kelvin.paopao.module.market.wallet.vip.VipActivity;
import com.kelvin.paopao.widgets.CustomToolbar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/10.
 */

public class WalletActivity extends BaseActivity<IBasePresenter> implements IWalletView {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.iv_vip)
    ImageView ivVip;
    @BindView(R.id.tv_money_status)
    TextView tvMoneyStatus;

    private boolean isVip;
    private String vipDate, money;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_wallet;
    }

    @Override
    protected void initInjector() {
        DaggerWalletComponent.builder().applicationComponent(getAppComponent())
                .walletModule(new WalletModule(this)).build().inject(this);
    }

    @Override
    protected void initViews() {
        customToolbar.showBackIcon(this);
        customToolbar.inflateMenu(R.menu.menu_wallet);
        customToolbar.setMenuOnClick(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                WalletDetailActivity.launch(WalletActivity.this);
                return true;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getData(false);
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick({R.id.tv_charge, R.id.iv_vip,R.id.tv_draw})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_charge:
                ChargeActivity.launch(this);
                break;
            case R.id.iv_vip:
                if (!TextUtils.isEmpty(money)) {
                    VipActivity.launch(this, isVip, vipDate, money);
                }
                break;
            case R.id.tv_draw:
                RechargeActivity.launch(this);
                break;
        }

    }

    public static void launch(Context context) {
        Intent intent = new Intent(context, WalletActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void setVip(boolean isVip, String vipdate) {
        if (isVip) {
            ivVip.setImageResource(R.drawable.icon_is_vip);
            tvMoneyStatus.setText("你的会员将于" + vipdate.substring(0, vipdate.lastIndexOf(" "))
                    + "过期");
        } else {
            ivVip.setImageResource(R.drawable.icon_is_not_vip);
        }
        this.isVip = isVip;
        this.vipDate = vipdate;
    }

    @Override
    public void setMoney(String money) {
        tvMoney.setText(money);
        this.money = money;
    }

}
